package com.soujiayi.coupon;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.soujiayi.activity.R;

public class CouponSimpleAdapter extends BaseAdapter {
	private Context context;
	private List<Map<String, Object>> list;
	private int layoutId;
	private String[] itemName;
	private int[] itemId;
	private LayoutInflater layoutInflater;
	private ArrayList<Object> listviews;
	private AdapterListener mAdapterListener;
	private int mPosition;
	private View mConvertView;
	private boolean isLv = true;
	private int selected;

	public CouponSimpleAdapter(Context context, List<Map<String, Object>> data,
			int resource, String[] from, int[] to) {
		this.context = context;
		this.list = data;
		this.layoutId = resource;
		this.itemName = from;
		this.itemId = to;
		layoutInflater = LayoutInflater.from(context);
	}

	public CouponSimpleAdapter(Context context, List<Map<String, Object>> data,
			int resource, String[] from, int[] to,
			AdapterListener adapterListener) {
		this.context = context;
		this.list = data;
		this.layoutId = resource;
		this.itemName = from;
		this.itemId = to;
		layoutInflater = LayoutInflater.from(context);

		this.mAdapterListener = adapterListener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = layoutInflater.inflate(layoutId, null);
			listviews = new ArrayList<Object>();
			for (int i = 0; i < itemId.length; i++) {
				View v = convertView.findViewById(itemId[i]);
				listviews.add(v);
			}
			convertView.setTag(listviews);
		} else {
			listviews = (ArrayList<Object>) convertView.getTag();
		}
		for (int i = 0; i < listviews.size(); i++) {
			Object obj = listviews.get(i);

	 if (obj instanceof TextView) {
				String s = (String) list.get(position).get(itemName[i]);
				if (s != null) {

					((TextView) obj).setText((String) list.get(position).get(
							itemName[i]));
				} else {
					((TextView) obj).setText("暂无该信息！");
				}
			} 
		else if (obj instanceof ImageView) {
				Object obj2 = list.get(position).get(itemName[i]);
				if (obj2 instanceof Bitmap) {
					Bitmap b = (Bitmap) obj2;
					ImageView iv = (ImageView) obj;
					if (b != null && !b.isRecycled()) {
						iv.setImageBitmap(b);
					} else {
						iv.setImageResource(R.drawable.ic_launcher);

					}
				} else if (obj2 instanceof Drawable) {
					((ImageView) obj).setImageDrawable((Drawable) obj2);
				} else if (obj2 instanceof Integer) {
					((ImageView) obj).setImageResource((Integer) obj2);
				} else {
					((ImageView) obj).setImageResource(R.drawable.ic_launcher);
				}
			} 

			if (mAdapterListener != null) {
				mPosition = position;
				mConvertView = convertView;
				mAdapterListener.addListener(mPosition, mConvertView, selected);
			}
		}
		return convertView;
	}

	public interface AdapterListener {
		void addListener(int position, View convertView, Object... objects);
	}

	public boolean isLv() {
		return isLv;
	}

	public void setLv(boolean isLv) {
		this.isLv = isLv;
	}

	public void setmAdapterListener(AdapterListener mAdapterListener) {
		this.mAdapterListener = mAdapterListener;
	}

	public int getSelected() {
		return selected;
	}

	public void setSelected(int selected) {
		if (this.selected != selected) {
			this.selected = selected;
			notifyDataSetChanged();
		}
	}
}
